package com.dhr.sarcosuchus.daobase.mybatis.parser.impl;

import com.dhr.sarcosuchus.daobase.mybatis.parser.SqlParser;
import com.dhr.sarcosuchus.daobase.pojo.Page;
import com.dhr.sarcosuchus.daobase.util.StringUtil;

/**
 * Created by peter.pan on 2016/12/6.
 *
 */

public abstract class AbstractSqlParser implements SqlParser {

    public AbstractSqlParser() {
    }


    protected String getSortSql(Page page) {
        if(this.isSortFieldEmptyOrIllegal(page)) {
            return "";
        } else {
            StringBuilder sortSql = new StringBuilder(" order by ");
            String[] sortFields = page.getSortField().split(",");
            String[] sortFieldTypes = page.getSortFieldType() == null?new String[0]:page.getSortFieldType().split(",");
            String[] sortOrders = this.getSortOrderArray(page);

            for(int i = 0; i < sortFields.length; ++i) {
                String sortField = sortFields[i];
                if(StringUtil.isEmpty(sortField)) {
                    break;
                }

                this.appendSortField(sortSql, sortField, i >= sortFieldTypes.length?null:sortFieldTypes[i]);
                if(sortOrders != null) {
                    sortSql.append(" ").append(sortOrders[i]);
                }

                sortSql.append(",");
            }

            return sortSql.deleteCharAt(sortSql.length() - 1).toString();
        }
    }

    private void appendSortField(StringBuilder sortSql, String sortField, String sortFieldType) {
        if(sortFieldType != null && !sortFieldType.equals("")) {
            String var4 = sortFieldType.toLowerCase();
            byte var5 = -1;
            switch(var4.hashCode()) {
                case 3886:
                    if(var4.equals("zh")) {
                        var5 = 0;
                    }
                    break;
                case 109446:
                    if(var4.equals("num")) {
                        var5 = 1;
                    }
            }

            switch(var5) {
                case 0:
                    this.getZhSortField(sortSql, sortField);
                    break;
                case 1:
                    this.getNumSortField(sortSql, sortField);
            }

        } else {
            sortSql.append(sortField);
        }
    }

    abstract void getZhSortField(StringBuilder var1, String var2);

    abstract void getNumSortField(StringBuilder var1, String var2);

    private String[] getSortOrderArray(Page page) {
        String[] sortOrders;
        if(StringUtil.isEmpty(page.getSortOrder())) {
            sortOrders = null;
        } else {
            if(page.getSortOrder().contains(" ")) {
                throw new IllegalArgumentException("sortOrder不能包含空格");
            }

            sortOrders = page.getSortOrder().split(",");
        }

        return sortOrders;
    }

    private boolean isSortFieldEmptyOrIllegal(Page page) {
        if(StringUtil.isEmpty(page.getSortField())) {
            return true;
        } else if(page.getSortField().contains(" ")) {
            throw new IllegalArgumentException("sortField不能包含空格");
        } else {
            return false;
        }
    }

}
